import createjs from "createjs-ts"
import cjs=createjs
import './index.scss'
import { createStage } from './helper/CreateJSHelper'
import ExportRoot from './ExportRoot'
import { GameConfig, ScaleMode } from './Const'
import { rewrite } from "./ext"

//console.log(cjs.Stage.prototype)
const init=()=>{
    //const gamediv=document.getElementById('gameDiv') as HTMLDivElement
    //console.log('gameDiv',gamediv.clientWidth,gamediv.clientHeight)
    const canvas=document.getElementById('stage') as HTMLCanvasElement
    console.log('canvas',canvas.clientWidth,canvas.clientHeight)
    const onresize=(e?:Event)=>{
        const ratio=window.devicePixelRatio
        console.log('ratio',ratio)
        /*if(GameConfig.screen_rotate==0){
            canvas.width=GameConfig.stageWidth=canvas.clientWidth*ratio
            canvas.height=GameConfig.stageHeight=canvas.clientHeight*ratio
        }else{
            canvas.width=GameConfig.stageHeight=canvas.clientHeight*ratio
            canvas.height=GameConfig.stageWidth=canvas.clientWidth*ratio
        }*/
        canvas.width=GameConfig.stageWidth=canvas.clientWidth*ratio
        canvas.height=GameConfig.stageHeight=canvas.clientHeight*ratio
        console.log('canvas',canvas.width,canvas.height)
    }
    /** 移动端不需要监听resize事件。 */
    //window.addEventListener('resize',onresize)
    onresize()
    //改写createjs
    //rewrite()
    const stage=createStage(canvas,GameConfig.frameRate)
    let scale=1
    if(GameConfig.scaleMode==ScaleMode.FIT_WIDTH){
        // 适配宽度
        scale=canvas.width/GameConfig.designWidth
        GameConfig.visibleWidth=GameConfig.designWidth
        GameConfig.visibleHeight=canvas.height/scale
        //stage.setBounds(0,0,GameConfig.designWidth,canvas.height/scale)
    }else if(GameConfig.scaleMode==ScaleMode.FIT_HEIGHT){
        // 适配高度
        scale=canvas.height/GameConfig.designHeight
        GameConfig.visibleWidth=canvas.width/scale
        GameConfig.visibleHeight=GameConfig.designHeight
        //stage.setBounds(0,0,canvas.width/scale,GameConfig.designHeight)
    }
    GameConfig.mainContainerScale=scale
    console.log('根容器缩放：',scale)
    const root=new ExportRoot()

    //双指缩放demo
    //const root=new PinchToZoomDemo()

    root.setTransform(0,0,scale,scale)
    stage.addChild(root)
    //移除加载界面
    const splash=document.getElementById('splash') as HTMLDivElement
    splash.style.display='none'
}
document.onreadystatechange=()=>{
    console.log('document.readyState',document.readyState)
    if(document.readyState=='complete'){
        init()
    }
}